<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="105px"
    >
      <el-form-item label="合同主题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入合同主题"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户名称/编号" prop="customerKey">
        <el-input
          v-model="queryParams.customerKey"
          placeholder="请输入客户名称或编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="销售专员" prop="salesPerson">
        <el-input
          v-model="queryParams.salesPerson"
          placeholder="请输入销售专员"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户订单编号" prop="customerOrderNumber">
        <el-input
          v-model="queryParams.customerOrderNumber"
          placeholder="请输入客户订单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      
      <el-form-item label="单据状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择单据状态"
          clearable
          class="!w-240px"
        >
          <el-option 
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="approvalStatus">
        <el-select
          v-model="queryParams.approvalStatus"
          placeholder="请选择审核状态"
          clearable
          class="!w-240px"
        >
          <el-option 
            v-for="item in approvalStatusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="销售类型" prop="saleType">
        <el-select
          v-model="queryParams.saleType"
          placeholder="请选择销售类型"
          clearable
          class="!w-240px"
        >
          <el-option 
            v-for="item in saleTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      
      <el-form-item class="ml-20px">
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
       <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['erp:sale-order:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['erp:sale-order:export']"
        >
          <Icon icon="ep:upload" />导出
        </el-button>
         <el-button size="default" @click="handleImport">
          <Icon icon="ep:download" class="mr-5px" /> 导入
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"   @selection-change="handleSelectionChange">
       <el-table-column width="30" label="选择" type="selection" />
      <el-table-column label="销售类型" align="center" prop="saleType" />
      <el-table-column label="合同编号" align="center" prop="contractNumber" />
      <el-table-column label="合同主题" align="center" prop="title" min-width="120" />
      <el-table-column label="客户编号" align="center" prop="customerCode" />
      <el-table-column label="客户名称" align="center" prop="customerName" min-width="120" />
      <el-table-column label="合同金额" align="center" prop="amount" min-width="100">
        <template #default="{ row }">
          {{ row.amount.toLocaleString('zh-CN') }}
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="approvalStatus">
        <template #default="{ row }">
          <el-tag :type="row.approvalStatus === '0' ? 'success' : 'danger'">
            {{ approvalStatusMap[row.approvalStatus] || row.approvalStatus }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="单据状态" align="center" prop="status">
        <template #default="{ row }">
          <el-tag :type="row.status === '0' ? 'success' : 'danger'">
            {{ statusMap[row.status] || row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="销售专员" align="center" prop="salesPerson" />
      <el-table-column label="签订日期" align="center" prop="signDate" :formatter="dateFormatter" width="110" />
      <el-table-column label="开始日期" align="center" prop="startDate" :formatter="dateFormatter" width="110" />
      <el-table-column label="客户订单编号" align="center" prop="customerOrderNumber" width="140" />
      <el-table-column label="终止时间" align="center" prop="endDate" :formatter="dateFormatter" width="110" />
      <el-table-column label="摘要" align="center" prop="summary" min-width="120" />
      <el-table-column label="是否过期" align="center" prop="isExpired">
        <template #default="{ row }">
          <el-tag :type="row.isExpired ? 'danger' : 'success'">
            {{ row.isExpired ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="合同使用状态" align="center" prop="usageStatus">
        <template #default="{ row }">
          {{ usageStatusMap[row.usageStatus] || row.usageStatus }}
        </template>
      </el-table-column>
      <el-table-column label="最后更新人" align="center" prop="updater" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="160"
      />
      
      <el-table-column label="操作" align="center" min-width="120px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['erp:sys-dict-item:update']"
          >
            编辑
          </el-button>
           <el-button
            link
            type="primary"
            @click="openForm('detail', scope.row.id)"
            v-hasPermi="['erp:sys-dict-item:update']"
          >
            详情
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['erp:sys-dict-item:delete']"
          >
            删除
          </el-button>
           <el-button
            link
            type="primary"
            @click="handleprint(scope.row.id)"
            v-hasPermi="['erp:sys-dict-item:delete']"
          >
            打印
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ContractForm ref="formRef" @success="getList" /> 
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
// import { SysDictItemApi, SysDictItemVO } from '@/api/erp/sysdictitem'
import ContractForm from './contractForm.vue'

/** 字典项 列表 */
defineOptions({ name: 'SysDictItem' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数

// 查询参数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  // 新增的查询参数
  title: undefined,
  customerKey: undefined,
  salesPerson: undefined,
  customerOrderNumber: undefined,
  status: undefined,
  approvalStatus: undefined,
  saleType: undefined,
  createTime: []
})

// 状态映射
const statusMap = {
  '0': '有效',
  '1': '无效'
}

const approvalStatusMap = {
  '0': '已审核',
  '1': '未审核'
}

const usageStatusMap = {
  '0': '在用',
  '1': '停用'
}

// 下拉选项
const statusOptions = [
  { value: '0', label: '有效' },
  { value: '1', label: '无效' }
]

const approvalStatusOptions = [
  { value: '0', label: '已审核' },
  { value: '1', label: '未审核' }
]

const saleTypeOptions = [
  { value: '1', label: '普通销售' },
  { value: '2', label: '直销' },
  { value: '3', label: '经销' },
  { value: '4', label: '代销' }
]
/** 选中操作 */
const selectionList = ref<any>([])
const handleSelectionChange = (rows: []) => {
  selectionList.value = rows
}
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    // 注意：这里需要根据实际接口修改请求参数和响应处理
    const data = await SysDictItemApi.getSysDictItemPage(queryParams)
    list.value = data.list
    total.value = data.total
    
    // 模拟数据
    if (list.value.length === 0) {
      list.value = [
        {
          id: 1,
          saleType: '普通销售',
          contractNumber: 'HT20240001',
          title: '2024年度销售合同',
          customerCode: 'C001',
          customerName: 'XX科技公司',
          amount: 500000,
          approvalStatus: '0',
          status: '0',
          salesPerson: '张三',
          signDate: '2024-01-15',
          startDate: '2024-02-01',
          customerOrderNumber: 'DD2024001',
          endDate: '2025-01-31',
          summary: '全年产品供应合同',
          isExpired: false,
          usageStatus: '在用',
          updater: '李四',
          createTime: '2024-01-10 14:30:00'
        }
      ]
      total.value = 1
    }
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SysDictItemApi.deleteSysDictItem(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SysDictItemApi.exportSysDictItem(queryParams)
    download.excel(data, '合同数据.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
const handleprint=()=>{
  
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>